/**
 * @file   1028.cpp
 * @author Shuang Hu <hsmath@ubuntu>
 * @date   Sun May 16 18:01:02 2021
 * 
 * @brief  PAT 1028:List Sorting
 * 
 * 
 */
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
class Student{
public:
    string ID;
    string name;
    int score;
};
bool cmp1(Student S1,Student S2){
    return S1.ID<S2.ID;
}
bool cmp2(Student S1,Student S2){
    if(S1.name==S2.name){
	return S1.ID<S2.ID;
    }
    return S1.name<S2.name;
}
bool cmp3(Student S1,Student S2){
    if(S1.score==S2.score){
	return S1.ID<S2.ID;
    }
    return S1.score<S2.score;
}
int main(){
    vector<Student> candidate;
    int stunum,type;
    cin>>stunum>>type;
    for(int i=0;i<stunum;i++){
	Student S;
	cin>>S.ID>>S.name>>S.score;
	candidate.push_back(S);
    }
    switch(type){
    case 1:
	sort(candidate.begin(),candidate.end(),cmp1);
	break;
    case 2:
	sort(candidate.begin(),candidate.end(),cmp2);
	break;
    case 3:
	sort(candidate.begin(),candidate.end(),cmp3);
	break;
    }
    for(int i=0;i<stunum;i++){
	printf("%s %s %d\n",candidate[i].ID.c_str(),candidate[i].name.c_str(),candidate[i].score);
	//cout<<candidate[i].ID<<" "<<candidate[i].name<<" "<<candidate[i].score<<endl;
    }
}
